package org.muninn.niagara.math;

import javax.baja.sys.BComponent;
import javax.baja.sys.BIcon;
import javax.baja.sys.BInteger;
import javax.baja.sys.Context;
import javax.baja.sys.Flags;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;

public class BConstants extends BComponent
{
  /** Value to start count from **/
  public static final Property startAt = newProperty(Flags.SUMMARY, 0);
  public void setStartAt(int v) { setInt(startAt, v, null); }
  public int getStartAt() { return getInt(startAt); }
  
  /** Total number of outputs **/
  public static final Property total = newProperty(Flags.SUMMARY, 16);
  public void setTotal(int v) { setInt(total, v, null); }
  public int getTotal() { return getInt(total); }
  
  /** Increment between outputs **/
  public static final Property countBy = newProperty(Flags.SUMMARY, 1);
  public void setCountBy(int v) { setInt(countBy, v, null); }
  public int getCountBy() { return getInt(countBy); }
  
  private static int start;
  private static int tot;
  private static int inc;
  
  public void started()
  {
    setup();
  }
  
  public void changed(Property p, Context cx)
  {
    if (!isRunning())
      return;
    
    if (p.equals(startAt) || p.equals(total) || p.equals(countBy))
      setup();
  }
  
  public void setup()
  {
    start = getStartAt();
    tot = getTotal();
    inc = getCountBy();
    
    this.removeAll();
    
    int k = start;
    for (int i = 0; i < tot; i++)
    {
      this.add("out" + k, BInteger.make(k), (Flags.SUMMARY|Flags.READONLY));
      k += inc;
    }
  }
  
  public BIcon getIcon() { return icon; }
  private static final BIcon icon = BIcon.make("local:|module://muninn/org/muninn/niagara/graphics/MuninnGroup.PNG");

  public static final Type TYPE = Sys.loadType(BConstants.class);
  public Type getType() { return TYPE; }
}
